package data_structure;

import java.util.ArrayList;
import java.util.List;

public class leetcode_0102 {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> list=new ArrayList<>();
        List<Integer> path=new ArrayList<>();
        List<TreeNode> stack=new ArrayList<>();
        int count=1;
        int current=0;
        if(root==null) return list;
        stack.add(root);
        while(count!=0){
            for(int i=0;i<count;++i){
                TreeNode node=stack.remove(0);
                if(node.left!=null){
                    stack.add(node.left);
                    current++;
                }
                if(node.right!=null){
                    stack.add(node.right);
                    current++;
                }
                path.add(node.val);
            }
            list.add(new ArrayList<>(path));
            path.clear();
            count=current;
            current=0;
        }
        return list;
    }
}
 class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }